﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using com.iss.inventory.Controllers;

namespace Staionery_Inventory.Presentation.Admin
{
    public partial class PersonalInformationUI__Reload_ : System.Web.UI.Page
    {
        PersonalInformationController perCon = new PersonalInformationController();
        //must get the ID from login information
        private String employeeID;

        protected void Page_Load(object sender, EventArgs e)
        {
            //security and current user name
            if (Session["empID"] == null)
            {
                Response.Redirect("~/Presentation/login.aspx");
            }

            Label lbl = this.Master.FindControl("lblLogInName") as Label;
            if (lbl != null)
            {
                lbl.Text = Session["UserName"].ToString();
            }

            employeeID = Convert.ToString(Session["empID"]);
            if (employeeID == null) { lblMessage.Text = "You must first log in"; }
            else {
                String[] emp = perCon.getEmpByID(employeeID);
                if (!IsPostBack) {
                    setDataToTextboxes(emp);
                }
            }
            
        }

        public void setDataToTextboxes(String[] emp)
        {
            lblName.Text = emp[0]; lblRole.Text = emp[1]; lblDeptID.Text = emp[2]; lblHeadId.Text = emp[3]; txtPhone.Text = emp[4]; txtEmail.Text = emp[5];
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            if (btnUpdate.Text == "Update")
            {
                txtPhone.ReadOnly = false; txtEmail.ReadOnly = false; btnUpdate.Text = "Save";
            }
            else if (btnUpdate.Text == "Save")
            {
                String result = perCon.updateEmp(employeeID, txtPhone.Text, txtEmail.Text);
                lblMessage.ForeColor = Color.Black;
                lblMessage.Text = result;
                txtPhone.ReadOnly = true; txtEmail.ReadOnly = true;
                btnUpdate.Text = "Update";
            }
        }

        protected void btnPassword_Click(object sender, EventArgs e)
        {
            if (btnPassword.Text == "Change") {
                btnPassword.Text = "Confirm"; txtPassword.Text = ""; txtPassword.ReadOnly = false;
                lblMessage.ForeColor = Color.Black; lblMessage.Text = "Please enter the previous password";
            }
            else if (btnPassword.Text == "Confirm") {
                String password = perCon.getEmpByID(employeeID)[6];
                if (password == txtPassword.Text)
                {
                    txtPassword.Text = ""; btnPassword.Text = "New";
                    txtPassword.ReadOnly = false;
                    lblMessage.ForeColor = Color.Black; lblMessage.Text = "Please enter the new password";
                }
                else {
                    lblMessage.ForeColor = Color.Red;lblMessage.Text = "Password incorrect";
                    btnPassword.Text = "Change"; txtPassword.Text = "********";
                    txtPassword.ReadOnly = true;
                }
            }
            else if (btnPassword.Text == "New") {
                String result=perCon.updatePassword(employeeID, txtPassword.Text);
                lblMessage.ForeColor = Color.Black; lblMessage.Text = result;
                btnPassword.Text = "Change"; txtPassword.ReadOnly = true;
            }
        }
    }
}